package com.rbn.stockmobiledevices.proveedores;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ProveedorController {
	
	private ProveedorManager proveedorManager;
	
	@Autowired 
	public ProveedorController(ProveedorManager proveedorManager) {
		this.proveedorManager = proveedorManager;
	}
	
	@RequestMapping(value="/getProveedorDetail", method=RequestMethod.GET)
	public @ResponseBody Proveedor getDetail(@RequestParam int id) {
		Proveedor p = proveedorManager.getById(id);
	    return p;
	}
	
	@RequestMapping(value="/removeProveedor", method=RequestMethod.GET)
	public @ResponseBody Map<String, Boolean> remove(@RequestParam int id) {
		boolean result = proveedorManager.remove(id);
		HashMap<String, Boolean> valueReturn = new HashMap<String, Boolean>();
		valueReturn.put("status", result);
	    return valueReturn;
	}
	
	@RequestMapping(value="/updateProveedor", method=RequestMethod.GET)
	public @ResponseBody Map<String, Boolean> update(@RequestParam int id, String nombre, String telefono, String email) {
		Proveedor proveedor = proveedorManager.getById(id);
		proveedor.setNombre(nombre);
		proveedor.setTelefono(telefono);
		proveedor.setEmail(email);

		HashMap<String, Boolean> valueReturn = new HashMap<String, Boolean>();
		boolean result = true;
		
		try{
			proveedorManager.update(proveedor);
		}catch(Exception e){
			result = false;
		}
		valueReturn.put("status", result);
	    return valueReturn;
	}
	
	@RequestMapping(value="/addProveedor", method=RequestMethod.GET)
	public @ResponseBody Map<String, Object> add(@RequestParam String nombre, @RequestParam String telefono, @RequestParam String email) {
		boolean result = true;
		Proveedor proveedor = new Proveedor();
		int provedor_id = 0;
		try{
			proveedor.setNombre(nombre);
			proveedor.setTelefono(telefono);
			proveedor.setEmail(email);
			provedor_id = proveedorManager.save(proveedor);
		}catch (Exception e) {
			result = false;
		}
		HashMap<String, Object> valueReturn = new HashMap<String, Object>();
		valueReturn.put("status", result);
		valueReturn.put("proveedor_id", provedor_id);
		
	    return valueReturn;
	}
	
	@RequestMapping(value = "/proveedorList", method = RequestMethod.GET)
	public String proveedorList(Model model) {
		return "proveedores.tiles";
	}
	
	@RequestMapping(value = "/proveedores", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, Object> getListaProveedores(@RequestParam int iDisplayStart, @RequestParam int iDisplayLength,
			@RequestParam String sSearch, @RequestParam String sEcho) {

		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Proveedor> proveedores = new ArrayList<Proveedor>();
		int total = 0;
		int filtered = 0;
		
		try{
			proveedores = proveedorManager.getAll();
			total = proveedores.size();
			filtered = total;
			
			if (sSearch != null && sSearch.trim().length() > 0) {
				filtered = proveedorManager.getByNombre(sSearch).size();
			}
			proveedores = proveedorManager.getPaginadoProveedores(iDisplayStart, iDisplayLength, sSearch);
		}catch(Exception e){
		}
		map.put("sEcho", sEcho);
		map.put("iTotalRecords", String.valueOf(total));
		map.put("iTotalDisplayRecords", String.valueOf(filtered));
		map.put("proveedores", proveedores);

		return map;
	}
}